package com.primaryedu.ui.app.growstep;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

import com.primaryedu.R;
import com.primaryedu.ui.BaseActivity;

/**
 * Created by Thomas on 2018/3/1.
 */

public class VideoActivity extends BaseActivity implements View.OnClickListener {
    private VideoView vv_video;
    private String video_path = "";
    private String isclassdynamic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_dynamic_video);
        init();
    }

    private void init() {
        Intent intent = getIntent();
        video_path = intent.getStringExtra("video_path");
        isclassdynamic = intent.getStringExtra("isclassdynamic");
        ((TextView) findViewById(R.id.title)).setText("1/1");
        findViewById(R.id.back).setOnClickListener(this);
        findViewById(R.id.right).setVisibility(View.VISIBLE);
        findViewById(R.id.right).setOnClickListener(this);
        if (isclassdynamic.equals("1")) {
            ((TextView) findViewById(R.id.text_right)).setText("");
            ((TextView) findViewById(R.id.text_right)).setEnabled(false);
        } else {
            ((TextView) findViewById(R.id.text_right)).setText("删除");
            ((TextView) findViewById(R.id.text_right)).setOnClickListener(this);
            ((TextView) findViewById(R.id.text_right)).setTextColor(getResources().getColor(R.color.main_button));
        }

        vv_video = (VideoView) findViewById(R.id.vv_video_video);
        vv_video.setOnClickListener(this);
        vv_video.setVideoPath(video_path);
        vv_video.start();
        //监听视频播放完的代码
        vv_video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mPlayer) {
                // TODO Auto-generated method stub
                mPlayer.start();
                mPlayer.setLooping(true);
            }
        });
    }

    @Override
    protected int BindLayout() {
        return R.layout.activity_dynamic_video;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.text_right:
                DynamicPublishActivity.DynamicPublishActivity.finish();
                intent = new Intent(VideoActivity.this, DynamicPublishActivity.class);
                startActivity(intent);
                finish();
            default:
                break;
        }
    }

    public void playVideo(String path) {
//     String path =Environment.getExternalStorageDirectory().getPath() + File.separator +"test.mp4";
        vv_video.setVideoPath(path);
        MediaController mctrl = new MediaController(this);
        vv_video.setMediaController(mctrl);

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.FILL_PARENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        vv_video.setLayoutParams(layoutParams);
        vv_video.start();
        vv_video.requestFocus();
    }
}
